<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: songaimin@clcw.com.cn  Blog:http://blog.csdn.net/samxx8        |
// +------------------------------------------------------------------------+
//

namespace AuctionCore\CarDealer;

use AuctionCore\System\AuctionException;

/**
 * 提车人管理
 * @author       Eric Lee
 */
class TakeMan
{

    private $dealer_id = null;

    /**
     * 构造
     * @param    int $dealer_id 车商id
     * @return   void
     */
    public function __construct($dealer_id)
    {
        $this->dealer_id = $dealer_id;
    }

    /**
     * 提车人列表
     * @return   Object
     */
    public function get_list($limit = '5', $where = '')
    {
        return M('take_car_man')->where("dealer_id='" . $this->dealer_id . "' " . $where)->limit(0, $limit)->select();
    }

    /**
     * 获取提车人列表
     * @param $page
     * @param $limit
     * @param $params
     * @return array
     */
    public static function getList($page, $limit, $params)
    {
        $page = intval($page) < 1 ? 1 : intval($page);
        $limit = intval($limit) < 1 ? 10 : intval($limit);

        $cols = "man_id,real_name,sex,mobile,idcard,take_num,status,createtime,is_contact_person";
        $map = array();
        $map = $params;
        $order = "man_id ASC,is_contact_person DESC";
        $total = M('take_car_man')->where($map)->count();
        $list = M('take_car_man')->field($cols)
            ->where($map)
            ->page($page)
            ->order($order)
            ->limit($limit)
            ->select();
        $list_info = array();
//        echo M()->getLastSql();
        foreach ($list as $k => $v) {
            $list_info[$k]['man_id'] = $v['man_id'];
            $list_info[$k]['real_name'] = $v['real_name'];
            $list_info[$k]['sex'] = $v['sex'];
            $list_info[$k]['mobile'] = $v['mobile'];
            $list_info[$k]['idcard'] = $v['idcard'];
            $list_info[$k]['take_num'] = $v['take_num'];
            $list_info[$k]['status'] = $v['status'];
            $list_info[$k]['createtime'] = $v['createtime'];
            $list_info[$k]['is_contact_person'] = $v['is_contact_person'];
        }
        $result = array(
            'currentpage' => $page,
            'pagesize' => $limit,
            'maxcount' => $total,
            'maxpage' => ceil($total / $limit),
            'list' => $list_info
        );
        return $result;
    }

    /**
     * 获取默认提车人
     * @return \Think\mixed
     */
    public function get_default_man()
    {
        $map = array(
            'dealer_id' => $this->dealer_id,
            'is_contact_person' => 1
        );
        return M('take_car_man')->where($map)->find();
    }

    /**
     * 添加提车人,返回:提车人id
     * 1.签约商户自动把（联系人添加为）一名提车人，并不可修改。
     * 2.每个车商最多可添加5名提车人
     * 3.受限和清退商户禁止添加提车人
     * @param    Array $data 提车人信息数组
     * @return   int
     */
    public function add($data)
    {
        if (empty($this->dealer_id)) {
            AuctionException::make_throw('500001');
        }
        if (empty($data['real_name'])) {
            AuctionException::make_throw('500005');
        }
        $exists = M('take_car_man')->where("dealer_id = {$this->dealer_id} AND real_name='" . $data['real_name'] . "'")->find();
        if (!empty($exists)) {
            AuctionException::make_throw('500008');
        }
//        if (!empty($data['idcard'])) {
////            $condition['dealer_id'] = $this->dealer_id;
//            $condition['idcard'] = (string)$data['idcard'];
//            $count = M('take_car_man')->where($condition)->count();
//            if ($count > 0) {
//                AuctionException::make_throw('500020');
//            }
//        }
        $data['dealer_id'] = $this->dealer_id;
        $data['createtime'] = date('Y-m-d H:i:s');

        return M('take_car_man')->add($data);
    }

    /**
     * 查看提车人信息
     * @param unknown $real_name
     */
    public function man_info($real_name, $mobile)
    {
        if (empty($this->dealer_id) || empty($real_name) || empty($mobile)) {
            return false;
        }
        $map = array();
        $map['real_name'] = $real_name;
        $map['mobile'] = $mobile;
        $exists = M('take_car_man')->where($map)->find();
        return $exists;
    }

    /**
     * 提车人详细
     * @param    int $man_id 要详细的提车人id
     * @return   Array
     */
    public function detail($man_id)
    {
        if (empty($man_id)) {
            AuctionException::make_throw('500006');
        }
        return M('take_car_man')->find($man_id);
    }

    /**
     * 删除提车
     * @param    int $man_id 要删除的提车人id
     * @return   boolean
     */
    public function del($man_id)
    {
        if (empty($man_id)) {
            AuctionException::make_throw('500006');
        }
        $c = M('take_car_man')->field('man_id')->find($man_id);
        if (empty($c)) {
            AuctionException::make_throw('500007');
        }
        return M('take_car_man')->delete($man_id);
    }

    /**
     * 修改提车人信息
     * @param    Array $data 提车人信息数组
     * @return   boolean
     */
    public function edit($data)
    {
        $man_id = $data['man_id'];
        if (empty($man_id)) {
            AuctionException::make_throw('500006');
        }
        $c = M('take_car_man')->field('man_id,dealer_id,idcard')->find($man_id);
        if (empty($c)) {
            AuctionException::make_throw('500007');
        }
        $condition['man_id'] = array('neq', $c['man_id']);
//        if (!empty($data['idcard'])) {
//            $condition['idcard'] = array('eq', (string)$data['idcard']);
//        }
//        $count = M('take_car_man')->where($condition)->count();
//        if ($count > 0) {
//            AuctionException::make_throw("500020");
//        }
        return M('take_car_man')->where("man_id='" . $man_id . "'")->save($data);
    }

    //更新提车人提车数量
    public static function updateTakeNum($man_id)
    {
        if ($man_id < 1) {
            return false;
        }
        $take_car_man = M('take_car_man');
        return $take_car_man->where('man_id=' . $man_id)->setInc('take_num'); // 提车人的提车数量+1
    }
}